﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text.RegularExpressions;

using permit.App_Code.DataAccess;
using permit.App_Code.Entity;


namespace permit
{
    public partial class UserInfo : System.Web.UI.Page
    {
        UserAccess userAcc = new UserAccess();
        AssginAccess assAcc = new AssginAccess();
        DepartmentAccess depAcc = new DepartmentAccess();
        DataSet ds;

        protected void Page_Load(object sender, EventArgs e)
        {
            
                
            if (!Page.IsPostBack)
            {
                UserTable user = (UserTable)Session["UserTable"];
                if (user == null)
                {
                    Response.Write("<script>alert('请先登录！')</script>");
                    Response.Redirect("~/default.aspx");
                }

                if (!assAcc.IsUserAssgin(user.USER_ID))
                {
                    Response.Redirect("~/main/Main.aspx?Sturt=User");
                }

                DataSet ds = depAcc.GetDepartments();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["dep_AreaCode"].ToString() == user.DEP_AREACODE && user.DEP_AREACODE != "620100")
                    {
                        ddlDep.Items.Add(new ListItem(dr["dep_Name"].ToString(), dr["dep_ID"].ToString()));
                        break;
                    }
                    if (user.DEP_AREACODE == "620100")
                    {

                        ddlDep.Items.Add(new ListItem(dr["dep_Name"].ToString(), dr["dep_ID"].ToString()));
                    }

                }

                if (Request.QueryString["depid"] != null)
                {
                    ddlDep.SelectedValue = Request.QueryString["depid"];

                    ds = userAcc.GetUsers(Convert.ToInt32(ddlDep.SelectedValue));
                    gridUser.DataSource = ds.Tables[0];
                    gridUser.DataBind();
                }
                else
                {
                    ds = userAcc.GetUsers(user.DEP_ID);
                    gridUser.DataSource = ds.Tables[0];
                    gridUser.DataBind();
                }

            }
        }
        protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e)
        {
            ds = userAcc.GetUsers(Convert.ToInt32(ddlDep.SelectedValue));
            gridUser.DataSource = ds.Tables[0];
            gridUser.DataBind();
        }

        protected void ddlDep_DataBound(object sender, EventArgs e)
        {
            //ds = userAcc.GetUsers(Convert.ToInt32(ddlDep.SelectedValue));
            //gridUser.DataSource = ds.Tables[0];
            //gridUser.DataBind();
        }

        protected void gridUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridUser.PageIndex = e.NewPageIndex;
            ds = userAcc.GetUsers(Convert.ToInt32(ddlDep.SelectedValue));
            gridUser.DataSource = ds.Tables[0];
            gridUser.DataBind();
        }
    }
}
